package com.test.stream.init;

import com.test.stream.bean2.RequestContext;
import com.test.stream.utils.XmlBuilder;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Component
public class XmlCacheInfo implements InitializingBean {
    // key 存储的是 RequestContext 的id ; value存储的是RequestContext所有的moudle
    private static Map<String,RequestContext> cacheMap = new ConcurrentHashMap<>(16);

    @Override
    public void afterPropertiesSet() throws Exception {
        List<RequestContext> input = XmlBuilder.getRequestContexts("input", RequestContext.class);
        if (CollectionUtils.isEmpty(input)){
            return;
        }
        Map<String, RequestContext> collect = input.stream().collect(Collectors.toMap(RequestContext::getId,
                requestContext -> requestContext));
//        Map<String, RequestContext> collect = input.stream().collect(Collectors.toMap(RequestContext::getId, student -> student));
        for(Map.Entry<String, RequestContext> entry:collect.entrySet()){
            System.out.println("=>"+entry.getKey()+":");
            entry.getValue().getMoudleList().stream().forEach(e->{
                System.out.println("==>"+e.getId()+":");
                e.getParamList().stream().forEach(g->{
                    System.out.println("===>"+g.getKey()+"-"+g.getValue()+"-"+g.getDesc());
                });
            });
        }
        XmlCacheInfo.cacheMap = collect;
    }

    /**
     * 根据requestContextId 获取该配置文件下所有的moudle信息
     * @param requestContextId
     * @return RequestContext
     */
    public static RequestContext getXmlCache(String requestContextId){
        return cacheMap.get(requestContextId);
    }

}
